package com.mach.platform.sprider.dev;

import com.mach.platform.sprider.Conf;
import com.mach.platform.util.SqlHelper;
import org.joda.time.DateTime;

/*
    取得变频器数据抓取器,一个对象针对一个采集模块
 */
public class LuDianKunHanDev extends DataHelper implements Runnable{

    private final Integer inputPort;
    protected String devName;

    protected SqlHelper sqls;

    public LuDianKunHanDev(String ip, Integer port, Integer devID, String devName, Integer inputPort) {
        super(ip,port,devID);

        this.sqls  = Conf.getSqlHelper();

        this.devName = devName;

        this.inputPort = inputPort;
    }



    /*
      取变频器状态, Null则为无数据,0则为异常,1为正常
   */
    public Float getTemp() {
        Float result = null;
        //02,03,02,00,33,BC,51,01,02,01,01,60,48,
        //02,03,04,00,33,00,00,39,3C,
        try {
            Integer startAddr = 0x5F + this.inputPort;
            byte[] repdata = this.getDataRtu(startAddr,1);
            byte[] data = ByteUtil.subData(repdata,3,2);
            data = ByteUtil.reverse(data);
            Short rs = ByteUtil.getShort(data);
            result = rs * 1.0f / 1000.0f;
            //result = result ;
            if(result < 4.0) result = 4.0f;
            result = (result-4.0f)/16.0f*70.0f - 20.0f;
        } catch (Exception e) {
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }


        return result;
    }

    @Override
    public void run(){

        long preSec = 0;
        long nowSec = 0;
        while(true){

            Integer excepCode = 2;

            try {


                DateTime dateTime = new DateTime();
                int fyear = dateTime.getYear();
                int fmonth = dateTime.getMonthOfYear();
                int fday = dateTime.getDayOfMonth();
                int fhour = dateTime.getHourOfDay();
                int fmin = dateTime.getMinuteOfHour();
                int fsec = dateTime.getSecondOfMinute();

                Float temp = this.getTemp();


                try {
                    this.sqls.updateAsync("insert ignore into data_detail(dev_id, fyear, fmonth, fday, fhour, fmin, fsec,fdatetime, " +
                            "ld_temp) value" +
                            "('"+this.devName+"',"+fyear+","+fmonth+","+fday+","+fhour+","+fmin+","+fsec+","+"date_format(concat("+fyear+",'-',"+fmonth+",'-',"+fday+",' ',"+fhour+",':',"+fmin+",':',"+fsec+")," +
                            "'%Y-%m-%d %H:%i:%s'),"
                            +temp+")");
                } catch (Exception e) {
                    e.printStackTrace();
                }

                try {
                    this.sqls.updateAsync("replace into data_cur (dev_id,ld_temp,update_time) value " +
                            "( " +
                            "'"+this.devName+"',"+
                            temp + "," +
                            "date_format(concat("+fyear+",'-',"+fmonth+",'-',"+fday+",' ',"+fhour+",':',"+fmin+",':',"+fsec+")," +
                            "'%Y-%m-%d %H:%i:%s')" +
                            ")");
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

            preSec = nowSec;


        }


    }


    public static void main(String[] args){
        LuDianKunHanDev td = new LuDianKunHanDev("192.168.3.90",8004,1,
                "14#变频器（PA2、PO2工艺24H）",1);
        Float pl = td.getTemp();
        System.out.println("pl="+pl);
    }


}
